{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PyTorch Udacity\n",
    "### Lesson 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write a function that takes as input a list of numbers, and returns\n",
    "# the list of values given by the softmax function.\n",
    "def softmax(L):\n",
    "    num_list = L\n",
    "    exp_num_list = map(lambda x: exp(x), num_list)\n",
    "    print(exp_num_list)\n",
    "    return exp_num_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "softmax([2, 1, 0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(exp_num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.utils.extmath import softmax\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[ 0.50839931,  0.49767588,  0.51260159]])\n",
    "softmax(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_list = np.array([[2, 1, 0]])\n",
    "exp_list = np.exp(num_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(num_list)\n",
    "print(exp_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "softmax(exp_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.utils.extmath import softmax\n",
    "import numpy as np\n",
    "\n",
    "num_list = np.array([[2, 1, 0]])\n",
    "exp_list = np.exp(num_list)\n",
    "\n",
    "softmax(exp_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write a function that takes as input a list of numbers, and returns\n",
    "# the list of values given by the softmax function.\n",
    "def softmax(L):\n",
    "    num_list = np.array(L)\n",
    "    exp_list = np.exp(num_list)\n",
    "    print(exp_list)\n",
    "    print(np.sum(exp_list))\n",
    "    softmax_list = num_list / np.sum(exp_list)\n",
    "    print(softmax_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "softmax([2, 1, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myInt=10\n",
    "myList=[tmpList/myInt for tmpList in range(10,100,10)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Udacity code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write a function that takes as input a list of numbers, and returns\n",
    "# the list of values given by the softmax function.\n",
    "def softmax(L):\n",
    "    num_list = np.array(L)\n",
    "    exp_num_list = map(lambda x: exp(x), num_list)\n",
    "    print(num_list)\n",
    "    print(exp_num_list)\n",
    "    myList=[np.exp(x)/sum(np.exp(num_list)) for x in num_list]\n",
    "    print(myList)\n",
    "\n",
    "\n",
    "mylist=[2, 1, 0]\n",
    "softmax(mylist)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Udacity code: clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write a function that takes as input a list of numbers, and returns\n",
    "# the list of values given by the softmax function.\n",
    "def softmax(L):\n",
    "    num_list = np.array(L)\n",
    "    myList=[np.exp(x)/sum(np.exp(num_list)) for x in num_list]\n",
    "    return myList\n",
    "\n",
    "mylist=[2, 1, 0]\n",
    "output = softmax(mylist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.6652409557748219, 0.24472847105479764, 0.09003057317038046]\n"
     ]
    }
   ],
   "source": [
    "print(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cross-entropy:  works!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cross-entropy:  4.83\n",
      "Error        :  1.21\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Write a function that takes as input two lists Y, P,\n",
    "# and returns the float corresponding to their cross-entropy.\n",
    "def cross_entropy(Y, P):\n",
    "    Y_list = np.array(Y)\n",
    "    P_list = np.array(P)\n",
    "\n",
    "    prob_sum = 0\n",
    "    for y in range(len(P_list)):\n",
    "        sum_quantity = -(Y_list[y]*np.log(P_list[y]) + (1-Y_list[y])*np.log(1-P_list[y]))\n",
    "        prob_sum += sum_quantity\n",
    "    \n",
    "    error_val = prob_sum / len(P_list)\n",
    "    print('Cross-entropy:  {0:4.2f}'.format(prob_sum))\n",
    "    print('Error        :  {0:4.2f}'.format(error_val))\n",
    "    return prob_sum\n",
    "                         \n",
    "Y=[1,0,1,1]\n",
    "P=[0.4,0.6,0.1,0.5]\n",
    "cross_ent = cross_entropy(Y, P)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.828313737302301\n"
     ]
    }
   ],
   "source": [
    "print(cross_ent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cross-entropy:  0.69\n",
      "Error        :  0.23\n",
      "0.6851790109107685\n"
     ]
    }
   ],
   "source": [
    "Y = [1, 1, 0]\n",
    "P = [0.8, .7, 0.1]\n",
    "\n",
    "cross_ent = cross_entropy(Y, P)\n",
    "print(cross_ent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cross-entropy:  2.48\n",
      "Error        :  0.83\n",
      "2.4769384801388235\n"
     ]
    }
   ],
   "source": [
    "Y = [1, 1, 1]\n",
    "P = [0.7, .3, 0.4]\n",
    "\n",
    "cross_ent = cross_entropy(Y, P)\n",
    "print(cross_ent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
